﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Workflow2211A.Domain.Entities;
using Workflow2211A.Domain.IndependentBilling.CheckupItem;
using Workflow2211A.Domain.IndependentBilling.DrugCategory;
using Workflow2211A.Domain.IndependentBilling.MedicalInstitution;
using Workflow2211A.Domain.IndependentBilling.PrescriptionApplication;
using Workflow2211A.Domain.IndependentBilling.ShoppingCart;
using Workflow2211A.Domain.IndependentBilling.TestItem;
using Workflow2211A.Domain.RBAC;
using WorkflowCore.Persistence.EntityFramework.Models;
using WorkflowCore.Persistence.EntityFramework.Services;

namespace Workflow2211A.Domain
{
    public class Workflow2211Context : WorkflowDbContext
    {
        #region 工作流配置
        public DbSet<PersistedEvent> Event { get; set; }   
        public DbSet<PersistedExecutionError> ExecutionError { get; set; }   
        public DbSet<PersistedExecutionPointer> ExecutionPointer { get; set; }   
        public DbSet<PersistedExtensionAttribute> ExtensionAttribute { get; set; }   
        public DbSet<PersistedScheduledCommand> ScheduledCommand { get; set; }   
        public DbSet<PersistedSubscription> Subscription { get; set; }   
        public DbSet<PersistedWorkflow> Workflow { get; set; }   
        public DbSet<WorkflowDefinitionModel> Definition { get; set; }   
        public DbSet<AuditsModel> Audits { get; set; }
        //public DbSet<FormTemplateModel> FormTemplate { get; set; }
        public DbSet<WorkflowInstanceModel> WorkflowInstance { get; set; }
        #endregion

        #region RBAC配置
        /// <summary>
        /// 用户表
        /// </summary>
        public DbSet<UsersModel> Users { get; set; }
        /// <summary>
        /// 角色表
        /// </summary>
        public DbSet<RolesModel> Roles { get; set; }
        /// <summary>
        /// 权限表
        /// </summary>
        public DbSet<PermissionsModel> Permissionss { get; set; }
        /// <summary>
        /// 用户角色表
        /// </summary>
        public DbSet<UserRolesModel> UserRoles { get; set; }
        /// <summary>
        /// 角色权限表
        /// </summary>
        public DbSet<RolePermissionsModel> RolePermissions { get; set; }
        #endregion
        #region 自主开单
        /// <summary>
        /// 检查项目分类表
        /// </summary>
        public DbSet<CheckupCategoryModel> CheckupCategories { get; set; }
        /// <summary>
        /// 检查项目表
        /// </summary>
        public DbSet<CheckupItemModel> CheckupItems { get; set; }
        /// <summary>
        /// 药品分类
        /// </summary>
        public DbSet<DrugCategoryModel> DrugCategories { get; set; }
        /// <summary>
        /// 药品信息表
        /// </summary>
        public DbSet<DrugInfoModel> DrugInfos { get; set; }
        /// <summary>
        /// 医疗机构信息表
        /// </summary>
        public DbSet<MedicalInstitutionModel> MedicalInstitutions { get; set; }
        /// <summary>
        /// 检验项目分类表
        /// </summary>
        public DbSet<TestCategoryModel> TestCategories { get; set; }
        /// <summary>
        /// 检验项目信息表
        /// </summary>
        public DbSet<TestItemModel> TestItems { get; set; }
        /// <summary>
        /// 开单申请表
        /// </summary>
        public DbSet<PrescriptionApplicationModel> PrescriptionApplications { get; set; }
        /// <summary>
        /// 开单明细表
        /// </summary>
        public DbSet<PrescriptionDetailModel> PrescriptionDetails { get; set; }
        
        /// <summary>
        /// 购物车表
        /// </summary>
        public DbSet<ShoppingCartModel> ShoppingCarts { get; set; }
        
        /// <summary>
        /// 购物车项目表
        /// </summary>
        public DbSet<CartItemModel> CartItems { get; set; }
        #endregion

        //Program 中添加配置可能会报错，所以挪到这里进行配置连接字符串
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseMySql("Server=8.141.112.218;Uid=root;Pwd=zpj20040217;database=MedicalClinic", new MySqlServerVersion("5.7"));
        }

        protected override void ConfigureEventStorage(EntityTypeBuilder<PersistedEvent> builder)
        {
            builder.ToTable("Event");
        }

        protected override void ConfigureExecutionErrorStorage(EntityTypeBuilder<PersistedExecutionError> builder)
        {
            builder.ToTable("ExecutionError");

        }

        protected override void ConfigureExecutionPointerStorage(EntityTypeBuilder<PersistedExecutionPointer> builder)
        {
            builder.ToTable("ExecutionPointer");

        }

        protected override void ConfigureExetensionAttributeStorage(EntityTypeBuilder<PersistedExtensionAttribute> builder)
        {
            builder.ToTable("ExtensionAttribute");

        }

        protected override void ConfigureScheduledCommandStorage(EntityTypeBuilder<PersistedScheduledCommand> builder)
        {
            builder.ToTable("ScheduledCommand");

        }

        protected override void ConfigureSubscriptionStorage(EntityTypeBuilder<PersistedSubscription> builder)
        {
            builder.ToTable("Subscription");

        }

        protected override void ConfigureWorkflowStorage(EntityTypeBuilder<PersistedWorkflow> builder)
        {
            builder.ToTable("Workflow");

        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            //统一配置软删除过滤
            modelBuilder.Entity<WorkflowDefinitionModel>().HasQueryFilter(x=>!x.IsDeleted);
            modelBuilder.Entity<AuditsModel>().HasQueryFilter(x=>!x.IsDeleted);
        }
    }
}
